package com.example.mylibrary.testActivity.util;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;

public class wifiUtil {
    private static wifiUtil instance;
    WifiManager wifiManager;
    private wifiUtil(){ }
    public static wifiUtil getInstance(){
        if(instance == null) {  synchronized (wifiUtil.class) { if (instance == null) instance = new wifiUtil(); }}
        return instance;
    }
    /**
     *  检测wifi的开关状态
     * @return  true 是wifi已经打开  false wifi处于关闭状态
     */
    public boolean getWiFi(Context context){
        //获取wifi管理服务
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        return wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED?true:false;
    }
    /**
     * 打开WiFi
     * @return  true 是WiFi已经打开  false  WiFi未能打开
     */
    public void OpenWiFi(Context context){
        if (getWiFi(context)) return ;
        else  wifiManager.setWifiEnabled(true);
    }
    /**
     * 关闭WiFi
     * @return  true 是WiFi已经关闭  false WiFi未能关闭
     */
    public void CloseWiFi(Context context){
        if (!getWiFi(context)) return ;
        else  wifiManager.setWifiEnabled(false);
    }
}
